
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Teach,Ember-Template," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>link-to helper [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        link-to helper
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Teach/" rel="tag">Ember-Teach</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Template/" rel="tag">Ember-Template</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 7 分钟</span>
    </header>

    <div class="post-content">
      <h3 id="1，link-to助手常规使用"><a href="#1，link-to助手常规使用" class="headerlink" title="1，link-to助手常规使用"></a>1，link-to助手常规使用</h3><p><code>link-to</code>助手表达式渲染之后就是一个<code>a</code>标签。而<code>a</code>标签的<code>href</code>属性的值是根据路由生成的，与路由的设置是息息相关的。并且每个设置的路由名称都是有着对应的关系的。<br>为了演示效果，用命令生成了一个<code>route</code>（或者手动创建文件）并获取测试数据。本文结合路由设置，随便串讲了一些路由方面的知识，如果你能看懂最好了，看不懂也不要紧后面会有一整章介绍路由。</p>
<h4 id="1-增加子路由"><a href="#1-增加子路由" class="headerlink" title="1, 增加子路由"></a>1, 增加子路由</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routers.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> config <span class="keyword">from</span> <span class="string">&#x27;./config/environment&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> Router = Ember.Router.extend(&#123;</span><br><span class="line">  location: config.locationType </span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">Router.map(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.route(<span class="string">&#x27;posts&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.route(<span class="string">&#x27;detail&#x27;</span>, &#123;<span class="attr">path</span>: <span class="string">&#x27;/:post_id&#x27;</span>&#125;);  <span class="comment">//指定子路由，:post_id会自动转换为数据的id</span></span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router;</span><br></pre></td></tr></table></figure>
<p>如上述代码，在<code>posts</code>下增加一个子路由<code>detail</code>。并且指定路由名为<code>/:post_id</code>，<code>:post_id</code>是一个动态字段，一般情况下默认为<code>model</code>的<code>id</code>属性。经过模板渲染之后会得到类似于<code>posts/1</code>、<code>posts/2</code>这种形式的路由。</p>
<h4 id="2-在route初始化数据"><a href="#2-在route初始化数据" class="headerlink" title="2, 在route初始化数据"></a>2, 在route初始化数据</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Ember.$.getJSON(<span class="string">&#x27;https://api.github.com/repos/emberjs/ember.js/pulls&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>使用Ember提供的方法直接从远程获取测试数据。测试数据的格式可以用浏览器直接打开上面的URL就可以看到。</p>
<h4 id="3，添加显示的模板"><a href="#3，添加显示的模板" class="headerlink" title="3，添加显示的模板"></a>3，添加显示的模板</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  //  app/templates/posts.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;row&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-10 col-xs-10&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&quot;margin-top: 70px;&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;list-group&quot;</span>&gt;</span></span><br><span class="line">                &#123;&#123;#each model as |item|&#125;&#125;</span><br><span class="line">                    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item&quot;</span>&gt;</span></span><br><span class="line">                        <span class="comment">&lt;!--设置路由，路由的层级与router.js里定义的要一致 --&gt;</span></span><br><span class="line">                     &#123;&#123;#link-to &#x27;posts.detail&#x27; item&#125;&#125;</span><br><span class="line">                            &#123;&#123;item.title&#125;&#125;</span><br><span class="line">                        &#123;&#123;/link-to&#125;&#125;</span><br><span class="line">                    <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">                &#123;&#123;/each&#125;&#125;</span><br><span class="line">                <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>直接用<code>{{{{#each}}</code>遍历出所有的数据，并显示在界面上，由于数据比较多可能显示的比较慢，特别是页面刷新之后看到一片空白，请不要急着刷新页面，等待一下即可⋯⋯。下图是结果的一部分：</p>
<p><img src="/content/images/2016/03/24.png" alt="结果截图"></p>
<p>我们查看页面的源代码，可以看到<code>link-to</code>助手渲染之后的HTML代码，自动生成了URL，在<code>router.js</code>里配置的<code>post_id</code>渲染之后都被<code>model</code>的<code>id</code>属性值代替了。</p>
<p><img src="/content/images/2016/03/25.png" alt="run result"></p>
<p>如果你没有测试数据，你还可直接把<code>link-to</code>助手的<code>post_id</code>写死，可以直接把数据的<code>id</code>值设置到<code>link-to</code>助手上。在模板文件的<code>ul</code>标签下一行增加如下代码，在<code>link-to</code>助手中指定<code>id</code>为<code>1</code>：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 增加一条直接指定id的数据 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item&quot;</span>&gt;</span></span><br><span class="line">    &#123;&#123;#link-to &#x27;posts.detail&#x27; 1&#125;&#125;增加一条直接指定id的数据&#123;&#123;/link-to&#125;&#125;</span><br><span class="line">  <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>渲染之后的HTML代码如下:</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">id</span>=<span class="string">&quot;ember404&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/posts/1&quot;</span> <span class="attr">class</span>=<span class="string">&quot;ember-view&quot;</span>&gt;</span>增加一条直接指定id的数据</span><br><span class="line"><span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>可以看到与前面使用动态数据渲染之后的<code>href</code>的格式是一样的。如果你想设置某个<code>a</code>标签是激活状态，你可以直接在标签上增加一个CSS类（<code>class=”active”</code>）。</p>
<h3 id="2，link-to助手设置多个动态字段"><a href="#2，link-to助手设置多个动态字段" class="headerlink" title="2，link-to助手设置多个动态字段"></a>2，link-to助手设置多个动态字段</h3><p>开发中，路由的路径经常不是2层的（<code>post/1</code>）也有可能是多层次的（<code>post/1/comment</code>、<code>post/1/2</code>或者<code>post/1/comment/2</code>等等形式。），如果是这种形式的URL在<code>link-to</code>助手上又要怎么去定义呢？<br>老样子，在演示模板之前还是需要先构建好测试数据以及修改对应的路由设置，此时的路由设置是多层的，因为<code>link-to</code>助手渲染之后得到的href属性值就是根据路由生成的！！！这个必须要记得⋯⋯</p>
<h4 id="1-一个路由下有个多子路由"><a href="#1-一个路由下有个多子路由" class="headerlink" title="1. 一个路由下有个多子路由"></a>1. 一个路由下有个多子路由</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routers.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> config <span class="keyword">from</span> <span class="string">&#x27;./config/environment&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> Router = Ember.Router.extend(&#123;</span><br><span class="line">  location: config.locationType </span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">Router.map(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="comment">// this.route(&#x27;handlebarsConditionsExpRoute&#x27;);</span></span><br><span class="line">  <span class="comment">// this.route(&#x27;handlebars-each&#x27;);</span></span><br><span class="line">  <span class="comment">// this.route(&#x27;store-categories&#x27;);</span></span><br><span class="line">  <span class="comment">// this.route(&#x27;binding-element-attributes&#x27;);</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">//  link-to实例理由配置</span></span><br><span class="line">  <span class="comment">// this.route(&#x27;link-to-helper-example&#x27;, function() &#123;</span></span><br><span class="line">  <span class="comment">//  this.route(&#x27;edit&#x27;, &#123;path: &#x27;/:link-to-helper-example_id&#x27;&#125;);</span></span><br><span class="line">  <span class="comment">// &#125;);</span></span><br><span class="line"></span><br><span class="line">  <span class="built_in">this</span>.route(<span class="string">&#x27;posts&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">//指定子路由，:post_id会自动转换为数据的id</span></span><br><span class="line">    <span class="built_in">this</span>.route(<span class="string">&#x27;detail&#x27;</span>, &#123;<span class="attr">path</span>: <span class="string">&#x27;/:post_id&#x27;</span>&#125;, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">//增加一个comments路由</span></span><br><span class="line">      <span class="built_in">this</span>.route(<span class="string">&#x27;comments&#x27;</span>);</span><br><span class="line">      <span class="comment">// 第二个子路由comment，并且路由是个动态字段comment_id</span></span><br><span class="line">      <span class="built_in">this</span>.route(<span class="string">&#x27;comment&#x27;</span>, &#123;<span class="attr">path</span>: <span class="string">&#x27;/:comment_id&#x27;</span>&#125;);</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;);</span><br><span class="line">  </span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router;</span><br></pre></td></tr></table></figure>

<p>如果是上述配置，渲染之后得到的路由格式<code>posts/detail/comments</code>。由于获取远程数据比较慢直接注释掉<code>posts.js</code>里的<code>model</code>回调方法，就直接使用写死id的方式。<br>注意：上述配置中，在路由<code>detail</code>下有2个子路由，一个是<code>comments</code>，一个是<code>comment</code>，并且这个是一个动态段。由此模板渲染之后应该是有2种形式的URL。一种是<code>posts.detail.comments</code>（<code>posts/1/comments</code>），另一种是<code>posts.detail.comment</code>（<code>posts/1/2</code>）。如果能理解这个那<code>route</code>嵌套层次再多应该也能看明白了！</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  //  app/templates/posts.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;row&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-10 col-xs-10&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&quot;margin-top: 70px;&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;list-group&quot;</span>&gt;</span></span><br><span class="line">                    <span class="comment">&lt;!-- 增加一条直接指定id的数据 --&gt;</span></span><br><span class="line">                    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item&quot;</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">!—-</span> 注意此时只有一个动态段，所以只有一个数字<span class="attr">1</span>，<span class="attr">Ember</span>会根据顺序自动匹配到动态段的位置上。 <span class="attr">--</span>&gt;</span></span><br><span class="line">                        &#123;&#123;#link-to &#x27;posts.detail.comments&#x27; 1 class=&#x27;active&#x27;&#125;&#125;</span><br><span class="line">                        posts.detail.comments（posts/1/comments）形式</span><br><span class="line">                        &#123;&#123;/link-to&#125;&#125;</span><br><span class="line">                    <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">                    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item&quot;</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">!—-</span></span></span><br><span class="line"><span class="tag">                        注意此时有<span class="attr">2</span>个动态段，所以有<span class="attr">2</span>个数字<span class="attr">1</span>，<span class="attr">2</span>，<span class="attr">Ember</span>会根据顺序自动匹配到动态段的位置上。</span></span><br><span class="line"><span class="tag">                        第一个数字<span class="attr">1</span>会匹配到第一个动态段<span class="attr">post_id</span>上，第二个数字<span class="attr">2</span>会匹配到动态段<span class="attr">comment_id</span>上</span></span><br><span class="line"><span class="tag">                         <span class="attr">--</span>&gt;</span></span><br><span class="line">                        &#123;&#123;#link-to &#x27;posts.detail.comment&#x27; 1 2 class=&#x27;active&#x27;&#125;&#125;</span><br><span class="line">                        posts.detail.comment（posts/1/2）形式</span><br><span class="line">                        &#123;&#123;/link-to&#125;&#125;</span><br><span class="line">                    <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"></span><br><span class="line">                <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>渲染之后的结果如下：</p>
<p><img src="/content/images/2016/03/26.png" alt="run result"></p>
<p>如果是动态段的一般都是<code>model</code>的<code>id</code>代替，如果不是动态段的直接显示配置的路由名称。</p>
<h4 id="2-多层路由嵌套"><a href="#2-多层路由嵌套" class="headerlink" title="2, 多层路由嵌套"></a>2, 多层路由嵌套</h4><p>上面演示了多个子路由的情况，下面接着介绍一个路由有多个层次，并且是有个多个动态段和非动态段组成的情况。<br>首先修改路由配置，把<code>comments</code>设置为<code>detail</code>的子路由。并且在<code>comments</code>下在设置一个动态段的子路由<code>comment_id</code>。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routers.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> config <span class="keyword">from</span> <span class="string">&#x27;./config/environment&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> Router = Ember.Router.extend(&#123;</span><br><span class="line">  location: config.locationType </span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">Router.map(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.route(<span class="string">&#x27;posts&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">//指定子路由，:post_id会自动转换为数据的id</span></span><br><span class="line">    <span class="built_in">this</span>.route(<span class="string">&#x27;detail&#x27;</span>, &#123;<span class="attr">path</span>: <span class="string">&#x27;/:post_id&#x27;</span>&#125;, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="comment">//增加一个comments路由</span></span><br><span class="line">      <span class="built_in">this</span>.route(<span class="string">&#x27;comments&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">// 在comments下面再增加一个子路由comment，并且路由是个动态字段comment_id</span></span><br><span class="line">        <span class="built_in">this</span>.route(<span class="string">&#x27;comment&#x27;</span>, &#123;<span class="attr">path</span>: <span class="string">&#x27;/:comment_id&#x27;</span>&#125;);</span><br><span class="line">      &#125;);   </span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Router;</span><br></pre></td></tr></table></figure>
<p>模板使用路由的方式<code>posts.detail.comments.comment</code>。正常情况应该生成类似<code>posts/1/comments/2</code>这种格式的URL。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  //  app/templates/posts.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;row&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-10 col-xs-10&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&quot;margin-top: 70px;&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;list-group&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">                    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item&quot;</span>&gt;</span></span><br><span class="line">                        <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">                            一共设置了4层路由</span></span><br><span class="line"><span class="comment">                         --&gt;</span></span><br><span class="line">                        &#123;&#123;#link-to &#x27;posts.detail.comments.comment&#x27; 1 2 class=&#x27;active&#x27;&#125;&#125;</span><br><span class="line">                        posts.detail.comments.comment（posts/1/comments/2）形式</span><br><span class="line">                        &#123;&#123;/link-to&#125;&#125;</span><br><span class="line">                    <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"></span><br><span class="line">                <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>渲染之后得到的HTML如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;list-group&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item&quot;</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 一共设置了4层路由 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">id</span>=<span class="string">&quot;ember473&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/posts/1/comments/2&quot;</span> <span class="attr">class</span>=<span class="string">&quot;active ember-view&quot;</span>&gt;</span>                      posts.detail.comments.comment（posts/1/comments/2）形式</span><br><span class="line">    <span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">li</span>&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>结果正如我们预想的组成了4层路由（<code>/posts/1/comment/2</code>）。<br>补充内容。<br>对于上述第二点多层路由嵌套的情况你还可以使用下面的方式设置路由和模板，并且可用同时设置了<code>/posts/1/comments</code>和<code>/posts/1/comments/2</code>。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">this</span>.route(<span class="string">&#x27;posts&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">//指定子路由，:post_id会自动转换为数据的id</span></span><br><span class="line">    <span class="built_in">this</span>.route(<span class="string">&#x27;detail&#x27;</span>, &#123;<span class="attr">path</span>: <span class="string">&#x27;/:post_id&#x27;</span>&#125;, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">//增加一个comments路由</span></span><br><span class="line">        <span class="built_in">this</span>.route(<span class="string">&#x27;comments&#x27;</span>);</span><br><span class="line">        <span class="comment">// 路由调用：posts.detail.comment</span></span><br><span class="line">        <span class="comment">// 注意区分与前面的设置方式，detai渲染之后会被/:post_id替换，comment渲染之后直接被comments/:comment_id替换了，</span></span><br><span class="line">        <span class="comment">//会得到如posts/1/comments/2这种形式的URL</span></span><br><span class="line">        <span class="built_in">this</span>.route(<span class="string">&#x27;comment&#x27;</span>, &#123;<span class="attr">path</span>: <span class="string">&#x27;comments/:comment_id&#x27;</span>&#125;);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  //  app/templates/posts.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;row&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-10 col-xs-10&quot;</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&quot;margin-top: 70px;&quot;</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;list-group&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line">                    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item&quot;</span>&gt;</span></span><br><span class="line">                        <span class="tag">&lt;<span class="name">--</span> 设置的方式不变 <span class="attr">--</span>&gt;</span></span><br><span class="line">                    &#123;&#123;#link-to &#x27;posts.detail.comments&#x27; 1 class=&#x27;active&#x27;&#125;&#125;</span><br><span class="line">                        posts.detail.comments（/posts/1/comments）形式</span><br><span class="line">                        &#123;&#123;/link-to&#125;&#125;</span><br><span class="line">                    <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"></span><br><span class="line">                    <span class="tag">&lt;<span class="name">li</span> <span class="attr">class</span>=<span class="string">&quot;list-group-item&quot;</span>&gt;</span></span><br><span class="line">                        <span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">                            一共设置了4层路由，与前面的设置方式一样</span></span><br><span class="line"><span class="comment">                         --&gt;</span></span><br><span class="line">                        &#123;&#123;#link-to &#x27;posts.detail.comment&#x27; 1 2 class=&#x27;active&#x27;&#125;&#125;</span><br><span class="line">                        posts.detail.comments.comment（posts/1/comments/2）形式</span><br><span class="line">                        &#123;&#123;/link-to&#125;&#125;</span><br><span class="line">                    <span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">                <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>渲染之后的结果如下：</p>
<p><img src="/content/images/2016/03/27.png" alt="渲染之后的结果"></p>
<p>两种方式定义的路由渲染的结果是一样的，看个人喜欢了，定义方式也是非常灵活的。第一种定义方式看着比较清晰，看代码就知道是一层层的。但是需要写更多代码。第二种定义方式更加简洁，不过看不出嵌套的层次。<br>    对于上述route的设置如果还不能理解也不要紧，后面会有一整章是介绍路由的，然而你能结合link-to助手理解了路由设置对于后面route章节的学习是非常有帮助的。</p>
<h4 id="3-在link-to助手内增加额外属性"><a href="#3-在link-to助手内增加额外属性" class="headerlink" title="3, 在link-to助手内增加额外属性"></a>3, 在link-to助手内增加额外属性</h4><p><code>handlebars</code>允许你直接在<code>link-to</code>助手增加额外的属性，经过模板渲染之后<code>a</code>标签就有了增加的额外属性了。<br>比如你可用为<code>a</code>标签增加CSS的<code>class</code>。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#123;link-to &quot;show text info&quot; &#x27;posts.detail&#x27; 1 class=&quot;btn btn-primary&quot;&#125;&#125;</span><br><span class="line"></span><br><span class="line">&#123;&#123;#link-to &quot;posts.detail&quot; 1 class=&quot;btn btn-primary&quot;&#125;&#125;show text info&#123;&#123;/link-to&#125;&#125;</span><br></pre></td></tr></table></figure>
<p>上述两种写法都是可以的，渲染的结果也是一样的。渲染之后的HTML为：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">id</span>=<span class="string">&quot;ember434&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/posts/1&quot;</span> <span class="attr">class</span>=<span class="string">&quot;btn btn-primary ember-view&quot;</span>&gt;</span>show text info<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>注意：上述两种方式的写法所设置的参数的位置不能调换。但是官网没有看到这方面的说明，有可能是我的演示实例的问题，如果读者你的可用欢迎给我留言。<br>第一种方式，显示的文字必须放在最前面，并且中间的参数是路由设置，最有一个参数是额外的属性设置，如果你还要其他的属性需要设置仍然只能放在最后面。<br>第二章方式的参数位置也是有要求的，第一个参数必须是路由设置，后面的参数设置额外的属性。<br>对于渲染之后的HTML代码中出现标签<code>id</code>为<code>ember</code>，或者<code>ember-xxx</code>，这些属性都是Ember默认生成的，我们可以暂时不用理它。<br>综合，本来这篇是讲解<code>link-to</code>的，但是由于涉及到了<code>route</code>的配置就顺便讲讲，有点难度，主要在路由的嵌套这个知识点上，但是对于后面的<code>route</code>这一章的学习是很有帮助的，<code>route</code>的设置几乎都是为URL设置的。这两者是紧密关联的！<br><br><br>博文完整代码放在<a target="_blank" rel="noopener" href="https://github.com/ubuntuvim/my_emberjs_code">Github</a>（博文经过多次修改，博文上的代码与github代码可能又出入，不过影响不大！），如果你觉得博文对你有点用，请在github项目上给我点个<code>star</code>吧。您的肯定对我来说是最大的动力！！</p>

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/ember-teach/templetes/%E5%86%85%E7%BD%AEHelper/" rel="next" title="内置Helper">
          内置Helper
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/templetes/handlebars%E9%81%8D%E5%8E%86%E6%A0%87%E7%AD%BE/" rel="prev" title="handlebars遍历标签">
            handlebars遍历标签
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%EF%BC%8Clink-to%E5%8A%A9%E6%89%8B%E5%B8%B8%E8%A7%84%E4%BD%BF%E7%94%A8"><span class="toc-text">1，link-to助手常规使用</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E5%A2%9E%E5%8A%A0%E5%AD%90%E8%B7%AF%E7%94%B1"><span class="toc-text">1, 增加子路由</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%9C%A8route%E5%88%9D%E5%A7%8B%E5%8C%96%E6%95%B0%E6%8D%AE"><span class="toc-text">2, 在route初始化数据</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3%EF%BC%8C%E6%B7%BB%E5%8A%A0%E6%98%BE%E7%A4%BA%E7%9A%84%E6%A8%A1%E6%9D%BF"><span class="toc-text">3，添加显示的模板</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%EF%BC%8Clink-to%E5%8A%A9%E6%89%8B%E8%AE%BE%E7%BD%AE%E5%A4%9A%E4%B8%AA%E5%8A%A8%E6%80%81%E5%AD%97%E6%AE%B5"><span class="toc-text">2，link-to助手设置多个动态字段</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E4%B8%80%E4%B8%AA%E8%B7%AF%E7%94%B1%E4%B8%8B%E6%9C%89%E4%B8%AA%E5%A4%9A%E5%AD%90%E8%B7%AF%E7%94%B1"><span class="toc-text">1. 一个路由下有个多子路由</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%A4%9A%E5%B1%82%E8%B7%AF%E7%94%B1%E5%B5%8C%E5%A5%97"><span class="toc-text">2, 多层路由嵌套</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E5%9C%A8link-to%E5%8A%A9%E6%89%8B%E5%86%85%E5%A2%9E%E5%8A%A0%E9%A2%9D%E5%A4%96%E5%B1%9E%E6%80%A7"><span class="toc-text">3, 在link-to助手内增加额外属性</span></a></li></ol></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: 'link-to helper', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
